/**
 * MixSmart元素监听
 */
var MixElementListener = function(){}
/**
 * 表单必填监听
 * 标识
 * class="require"
 */
MixElementListener.formRequireListener = function() {
	$(".mix-require").each(function(){
		if(!$(this).prop("disabled")) {
			var id = $(this).attr("id");
			var newIdTag = id+"-"+"require";
			/*if(utils.isEmpty($(this).parent().find("#"+newIdTag).attr("class"))) {
				$(this).after("<span id='"+newIdTag+"' class='mix-star-require'> * </span>");
			}*/
			if(!$(this).next().hasClass("mix-star-require")) {
				$(this).after("<span id='"+newIdTag+"' class='mix-star-require'> * </span>");
			}
		}
	});
}

/**
 * 链接监听,也可以是按钮或其他
 * class="mix-change-page" 该标识主要是用来标记分页，点击页面时触发的事件
 *   参数:必须 data-uri 分页uri
 *       可选 data-target 显示地方(一般为一个div层)
 *   
 * class="mix-open-self" 点击时，指定的uri显示到当前"#main-content"里面
 *   参数：必须 data-uri 显示uri
 *        可选 data-target 显示地方(一般为一个div层)
 *        
 * class="mix-open-blank" 点击时，会弹出一个新窗口（弹出窗口）;  
 *    参数： 必须 data-uri 弹出页面的uri
 *         可选 data-title 弹出窗口的标题;data-width 弹出窗口的宽度
 *         
 */
MixElementListener.hrefListener = function() {
	//$(".mix-change-page").unbind("click")
	$(".mix-change-page").each(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-change-page-listener")) {
			$this.addClass("mix-change-page-listener");
			$this.click(function(event) {
				var uri = $(this).data("uri");
				uri = utils.isEmpty(uri)?$(this).attr("href"):uri;
				var target = $(this).data("target");
				if (!utils.isEmpty(uri)) {
					if(!utils.isEmpty(target))
						loadUri(target,uri,true);
					else 
						loadLocation(uri);
				}
				event.stopPropagation();
				return false;
			});
		}
		//$this = null;
	});
	
	//$(".mix-open-self").unbind("click")
	$(".mix-open-self").each(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-open-self-listener")) {
			$this.addClass("mix-open-self-listener");
			$this.click(function(event) {
				var uri = $(this).data("uri");
				var target = $(this).data("target");
				var title = $(this).data("title");
				if(utils.isEmpty(uri)) {
					uri = $(this).attr("href");
				}
				if (!utils.isEmpty(uri)) {
					if(!utils.isEmpty(target) && target != '#main-content')
						loadUri(target, uri, true);
					else 
						openTab(title, uri, true);
						//loadLocation(uri);
				}
				event.stopPropagation();
				return false;
			});
		}
		//$this = null;
	});
	
	//$(".mix-open-blank").unbind("click")
	$(".mix-open-blank").each(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-open-blank-listener")) {
			$this.addClass("mix-open-blank-listener");
			$this.click(function(event){
		        var uri = $(this).data("uri");
		        var title = $(this).data("title");
		        var w = $this.data("width");
		        if(!utils.isEmpty(uri)) {
		          if(utils.isEmpty(w)) {
		        	  w = $(window).width()-100;
		          }
		          BootstrapDialogUtil.loadUriDialog(title,uri,w,"#fff",false,function(){
						setTimeout(function(){
							initEvent();
						}, 200);
				  });
		        }
		        event.stopPropagation();
		        return false;
		     });
			
		}
		//$this = null;
	});
	
}

/**
 * 单击搜索按钮提交数据
 * 标识
 *   class="mix-search-submit" 标记在触发按钮上
 *   参数
 *     必须
 *       action 该参数为form表单的action属性；提交的路径url
 *     可选
 *       target 该参数为form表单的target属性；提交数据请求之后，返回内容显示的位置，默认为"#main-content"
 *       data-loading-target-tag 要获取载入页面指定的内容
 */
MixElementListener.searchSubmitListener = function(){
	//$(".mix-search-submit").unbind("click");
	$(".mix-search-submit").each(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-search-submit-listener")) {
			$this.addClass("mix-search-submit-listener");
			$this.click(function() {
			    var $form = $(this).parents("form:eq(0)");
				var param = $form.serialize();
			    var uri = $form.attr("action");
			    var target = $form.attr("target");
			    var loadingTargetTag = $form.data("loading-target-tag");
			    param = encodeURI(param);
			    if(!utils.isEmpty(uri)) {
			    	if(utils.isContain(uri, "?")) {
			    		 uri = uri+"&"+param;
			    	} else {
			    		 uri = uri+"?"+param;
			    	}
			    	uri = utils.isEmpty(loadingTargetTag)?uri:uri+" "+loadingTargetTag;
			 	    if(!utils.isEmpty(target) && mainTag != target) {
			 		    loadUri(target,uri);
			 	    } else {
			 		    loadLocation(uri);
			 	    }
			    }
			    return false;
			});
		}
		//$this = null;
	});
	
}

/**
 * 载入指定的uri到DIV标识为id的层中
 * @param id
 * @param uri
 * @param isLoadProcess 是否有加载等待提示,默认为:true
 * @param isCheckLogin 是否验证用户登录,默认为:true(访问页面时验证用户是否已登录)
 * @param callback 页面成功加载后的回调函数
 */
MixElementListener.loadUri = function(id,uri,isLoadProcess,isCheckLogin, callback) {
	if(false != isLoadProcess) {
		isLoadProcess = true;
	}
	if(utils.isEmpty(isCheckLogin)) {
		isCheckLogin = true;
	}
	isCheckLogin = isCheckLogin == false ? false : true;
	if(isLoadProcess) 
		utils.waitLoading("数据加载中...");
	if(isCheckLogin) {
		$.get("user/islogin.json",function(data){
			if(data.result=='1') {
				var array = utils.parseUri(uri);
				if(null != array) {
					$(id).load(array[0],array[1],function() {
						if(isLoadProcess)
							utils.closeWaitLoading();
						if(utils.isNotEmpty(callback) && typeof(callback) == 'function') {
							setTimeout(function(){
								callback.call(this);
							}, 300);
						}
					});
				}
			} else {
				location.reload();
			}
		});
	} else {
		var array = utils.parseUri(uri);
		if(null != array) {
			$(id).load(array[0],array[1],function() {
				if(isLoadProcess)
					utils.closeWaitLoading();
				if(utils.isNotEmpty(callback) && typeof(callback) == 'function') {
					setTimeout(function(){
						callback.call(this);
					}, 300);
				}
			});
		}
	}
}

/**
 * 监听表单输入框树
 * 标识
 *   class='mix-input-tree'
 * 参数
 *   必须
 *     data-uri 指定数据来源uri 
 *   可选
 *     data-is-show-none 是否显示"无"数据节点;默认为:no(不显示) 可选的值为:"yes"或"no"
 */
MixElementListener.inputTreeListener = function() {
	//$(".mix-input-tree").unbind("click");
	$(".mix-input-tree").each(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-input-tree-listener")) {
			$this.addClass("mix-input-tree-listener");
			var uri = $(this).data("uri");
			var isShowNode = $(this).data("is-show-none");
			isShowNode = (isShowNode == 'yes'?true:false);
			
			var isAjaxAsync = $(this).data("is-ajax-async");
			isAjaxAsync = (isAjaxAsync == 'yes'?true:false);
			
			var isAsync = $(this).data("is-async");
			isAsync = (isAsync == 'yes'?true:false);
			
			if(!utils.isEmpty(uri)) {
				$(this).zTreeUtil({
					uri:uri,
					isAsync : isAsync,
					isAjaxAsync : isAjaxAsync,
					isInput:true,
					isInputTreeShow:false,
					isShowNone:isShowNode
				});
			}
			$this.click(function(event){
				if(!utils.isEmpty(uri)) {
					$(this).zTreeUtil({
						uri:uri,
						isInput:true,
						isShowNone:isShowNode
					});
				}
				event.stopPropagation();
			});
		}
	});
}

/**
 * 监听表单组织机构树
 * 标识
 *   class='mix-input-org-tree'
 *   参数
 *   可选
 *     data-is-show-none 是否显示"无"数据节点;默认为:no(不显示) 可选的值为:"yes"或"no"
 */
MixElementListener.inputOrgTreeListener = function() {
	var uri = 'org/tree.json';
	$(".mix-input-org-tree").each(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-input-org-tree-listener")) {
			$this.addClass("mix-input-org-tree-listener");
			var isShowNode = $(this).data("is-show-none");
			isShowNode = (isShowNode == 'yes'?true:false);
			var isAjaxAsync = $(this).data("is-ajax-async");
			isAjaxAsync = (isAjaxAsync == 'yes'?true:false);
			
			var isAsync = $(this).data("is-async");
			isAsync = (isAsync == 'yes'?true:false);
			if(!utils.isEmpty(uri)) {
				$(this).zTreeUtil({
					uri:uri,
					isInput:true,
					isAsync : isAsync,
					isAjaxAsync : isAjaxAsync,
					isInputTreeShow:false,
					isShowNone:isShowNode
				});
			}
			$this.click(function(event){
				if(!utils.isEmpty(uri)) {
					$(this).zTreeUtil({
						uri:uri,
						isInput:true,
						isShowNone:isShowNode
					});
				}
				event.stopPropagation();
			});
		}
	});
	
}


/**
 * 监听面板树
 * 标识
 *   class='mix-panel-tree'
 * 参数
 *   必须
 *     data-uri 指定数据来源uri
 *   可选
 *     data-redirect-uri 点击节点之后触发的uri
 *     data-is-search 是否支持搜索功能;yes--支持；no--不支持;默认为：no
 *     data-is-node-link 节点是否支持超链接;yes--支持;no--不支持；默认为：no
 *     data-is-default-load 是否默认加载data-redirect-uri指定的uri，该选项设置了data-is-node-link为"yes"的时候才生效
 *     data-target 指定uri内容显示的位置；如果该值为空或没设置，则默认显示到"#main-content"层，一般与"data-redirect-uri"成对出现;
 *     data-param-name 指定节点ID的参数名称，默认名称为："id"
 */
MixElementListener.panelTreeListener = function() {
	$(".mix-panel-tree").each(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-panel-tree-listener")) {
			$this.addClass("mix-panel-tree-listener");
			var uri = $(this).data("uri");
			var redirectUri = $(this).data("redirect-uri");
			var target = $(this).data("target");
			var isSearch = $(this).data("is-search");
			isSearch = utils.isEmpty(isSearch)?false:(isSearch=='yes'?true:false);
			var paramName = $(this).data("param-name");
			var isNodeLink = $(this).data("is-node-link");
			var panelHeight = $(this).data("panel-height");
			panelHeight = utils.regexInteger(panelHeight)?panelHeight:0;
			isNodeLink = utils.isEmpty(isNodeLink)?false:(isNodeLink == 'yes'?true:false);
			var isDefaultLoad = false;
			if(isNodeLink) {
				isDefaultLoad = $(this).data("is-default-load");
				isDefaultLoad = utils.isEmpty(isDefaultLoad)?false:(isDefaultLoad == 'yes'?true:false);
			}
			
			var isAjaxAsync = $(this).data("is-ajax-async");
			isAjaxAsync = (isAjaxAsync == 'yes'?true:false);
			
			var isAsync = $(this).data("is-async");
			isAsync = (isAsync == 'yes'?true:false);
			
			if(!utils.isEmpty(uri)) {
				$(this).zTreeUtil({
					uri:uri,
					isInput:false,
					isAsync : isAsync,
					isAjaxAsync : isAjaxAsync,
					redirectUri:redirectUri,
					target:target,
					isSearch:isSearch,
					panelHeight:panelHeight,
					isNodeLink:isNodeLink,
					isDefaultLoad:isDefaultLoad,
					paramName:paramName
				});
			}
		}
	});
}

/**
 * 监听还有复选框面板树
 * 标识
 *   class='mix-panel-check-tree'
 * 参数
 *   必须
 *     data-uri 指定数据来源uri
 *   可选
 *     data-is-search 是否支持搜索功能;值为"yes"或"no"
 *     data-param-name 指定节点ID的参数名称，默认名称为："id"
 *     data-check-opt 选项json格式;如:{check:{chkStyle:"radio",radioType: "level/all"}};
 *     data-param-name 指定节点ID的参数名称，默认名称为："id"
 */
MixElementListener.panelCheckTreeListener = function() {
	$(".mix-panel-check-tree").each(function(){	
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-panel-check-tree-listener")) {
			$this.addClass("mix-panel-check-tree-listener");
			var uri = $(this).data("uri");
			var isSearch = $(this).data("is-search");
			    isSearch = !utils.isEmpty(isSearch)?("yes"==isSearch?true:false):false;
			var checkOpt = $(this).data("check-opt");
			    checkOpt = !utils.isEmpty(checkOpt)?checkOpt:null;
			var paramName = $(this).data("param-name");
			    paramName = utils.isEmpty(paramName)?"id":paramName;
			var isAjaxAsync = $(this).data("is-ajax-async");
			isAjaxAsync = (isAjaxAsync == 'yes'?true:false);
			var isAsync = $(this).data("is-async");
			isAsync = (isAsync == 'yes'?true:false);
			    
			if(!utils.isEmpty(uri)) {
				$(this).zTreeUtil({
					uri:uri,
					isInput:false,
					isAsync : isAsync,
					isAjaxAsync : isAjaxAsync,
					isCheck:true,
					checkOpt:checkOpt,
					isSearch:isSearch,
					paramName:paramName
				});
			}
		}
	});
}

/**
 * 监听面板组织结构树
 * 标识
 *   class='mix-panel-org-tree'
 * 参数
 *   data-redirect-uri 添加节点之后触发的uri
 *   data-target 指定uri内容显示的位置；如果该值为空或没设置，则默认显示到"#main-content"层，一般与"data-redirect-uri"成对出现;
 *   data-param-name 指定节点ID的参数名称，默认名称为："id"
 * 
 */
MixElementListener.panelOrgTreeListener = function() {
	var uri = 'org/tree';
	$(".mix-panel-org-tree").each(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-panel-org-tree-listener")) {
			$this.addClass("mix-panel-org-tree-listener");
			var redirectUri = $(this).data("redirect-uri");
			var target = $(this).data("target");
			var isSearch = $(this).data("is-search");
			isSearch = utils.isEmpty(isSearch)?false:(isSearch=='yes'?true:false);
			var paramName = $(this).data("param-name");
			var isNodeLink = $(this).data("is-node-link");
			var panelHeight = $(this).data("panel-height");
			panelHeight = utils.regexInteger(panelHeight)?panelHeight:0;
			isNodeLink = utils.isEmpty(isNodeLink)?false:(isNodeLink == 'yes'?true:false);
			var isDefaultLoad = false;
			if(isNodeLink) {
				isDefaultLoad = $(this).data("is-default-load");
				isDefaultLoad = utils.isEmpty(isDefaultLoad)?false:(isDefaultLoad == 'yes'?true:false);
			}
			var isAjaxAsync = $(this).data("is-ajax-async");
			isAjaxAsync = (isAjaxAsync == 'yes'?true:false);
			var isAsync = $(this).data("is-async");
			isAsync = (isAsync == 'yes'?true:false);
			
			if(!utils.isEmpty(uri)) {
				$(this).zTreeUtil({
					uri:uri,
					isInput:false,
					isAsync : isAsync,
					isAjaxAsync : isAjaxAsync,
					redirectUri:redirectUri,
					target:target,
					isSearch:isSearch,
					panelHeight:panelHeight,
					isNodeLink:isNodeLink,
					isDefaultLoad:isDefaultLoad,
					paramName:paramName
				});
			}
		}
	});
}

/**
 * 提交按钮监听
 * 标识
 * class="mix-data-submit"
 * 该标识是提交表单触发的事件
 * 除了action参数在form表单里面填写之外，其他的参数都在该标识元素中填写
 * 参数
 *   必须
 *      action 该参数就是form表单的action属性,在提交的form的action里面填写该值,指定提交的路径(action)
 *   可选
 *      data-jqgrid-id 该参数指定jqgrid的ID值（如果列表用jqGrid插件的话，如果指定了data-fun参数，该参数会失效）
 *      data-is-iframe 是否在iframe中；如果是为：yes；否则为空或no
 *      data-target-form 该参数，指定提交哪个form表单的ID（有多个form表单时）;如:target-form="#add-form";
 *      data-fun 该参数是一个回调函数，提交成功后执行的方法;
 *      data-refresh-uri 该参数指定数据指定成功后，刷新的uri;
 *      target 该参数为form表单的target属性；提交数据请求之后，返回的内容显示到哪个里面，默认为"#main-content"
 *      与data-refresh-uri成对出现.
 *      
 */
MixElementListener.submitBtnListener = function() {
	$(".mix-data-submit").each(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-data-submit-listener")) {
			$this.addClass("mix-data-submit-listener");
			$this.click(function(event){
				var target = $(this).data("target-form");
				var fun = $(this).data("fun");
				var $form = null;
				if(!utils.isEmpty(target)) {
					$form = $(target);
					if(typeof($form.attr("id")) === 'undefined') {
						$form = null;
						console.error("data-target-form 必须指定form的id");
					}
				} else {
					 $form = $(this).parents("form:eq(0)");
				}
				if($form.validateForm()) {
					var param = $form.serialize();
				    var uri = $form.attr("action");
				    MixSmart.submitDialogData(uri,param,fun,$(this),$form);
				}
				event.stopPropagation();
				return false;
			});
		}
	});
}

/**
 * 监听限制高度标识
 * 标识
 * class="mix-auto-limit-height"
 * 该标识会根据浏览器高度，自适应被该标识标记的DIV层
 */
MixElementListener.limitHeightListener = function() {
	$(".mix-auto-limit-height").each(function(){
		var $limitHeight = $(this);
		if(!utils.isContain($limitHeight.attr("class"), "mix-auto-limit-height-listener")) {
			$limitHeight.addClass("mix-auto-limit-height-listener");
			var subtractHeight = $limitHeight.data("subtract-height");
			if(utils.isNotEmpty(subtractHeight)) {
				subtractHeight = parseInt(subtractHeight);
			} else {
				subtractHeight = 0;
			}
			var $mainPanel = $("#mix-main-content");
			var spacingH = $limitHeight.offset().top - $mainPanel.offset().top;
			var otherH = 0;
			var $parent = $limitHeight.parent();
			while(!$parent.is("div#mix-main-content")) {
				otherH = otherH + Math.ceil(($parent.outerHeight(true) - $parent.height())/2);
				$parent = $parent.parent();
			}
			var limitH = $limitHeight.outerHeight(true);
			var addH = getMainHeight()- spacingH - $limitHeight.outerHeight(true) - otherH - subtractHeight;
			$limitHeight.height(limitH+addH);
			$limitHeight.css({"overflow":"auto"});
		}
	});
}

/**
 * 监听文件上传标识
 * 标识
 * class="mix-upload"
 * 参数
 *   必须
 *     data-uri 上传到服务器上的action路径,如:data-uri="uploadAttr/uploadAttr"
 *   可选
 *     data-form-data 表单参数;填写表单ID；如:data-form-data="#form-id"
 *     data-accept-file-types 支持上传的文件类型;多个类型之间用英文逗号","分开;如:data-accept-file-types="gif,png,jpg"
 *     data-max-file-size 支持上传的文件最大大小;单位为:byte;如:data-max-file-size="1024000000000"
 *     data-pop-width 上传文件时弹出窗口的宽度
 *     data-pop-height 上传文件时弹出窗口的高度
 *     data-progressbar 是否显示上传进度条;1--显示；0--不显示
 *     data-close-after 文件上传结束后，关闭窗口时执行的回调函数;返回一个数组
 *     
 */
MixElementListener.uploadFileListener = function() {
	$(".mix-upload").each(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-upload-listener")) {
			$this.addClass("mix-upload-listener");
			var uri = $this.data("uri");
			var formData = $this.data("form-data");
			var acceptFileTypes = $this.data("accept-file-types");
			var maxFileSize = $this.data("max-file-size");
			var popWidth = $this.data("pop-width");
			var popHeight = $this.data("pop-height");
			var progressBar = $this.data("progressbar");
			var closeAfterFun = $this.data("close-after");
			if(!utils.isEmpty(uri)) {
				formData = utils.isEmpty(formData)?null:$(formData).serializeArray();
				popWidth = utils.isEmpty(popWidth)?450:popWidth;
				popHeight = utils.isEmpty(popHeight)?300:popHeight;
				progressBar = (utils.isEmpty(progressBar) || progressBar=='0')?false:true;
				closeAfterFun = utils.isEmpty(closeAfterFun)?null:eval(closeAfterFun);
				if(null != formData) {
					formData = JSON.stringify(param);
				}
				var param = {
						uri:uri,
						formData:formData,
						popWidth:popWidth,
						popHeight:popHeight,
						progressBar:progressBar,
						closeAfterFun:closeAfterFun
				};
				if(!utils.isEmpty(acceptFileTypes)) {
					acceptFileTypes = acceptFileTypes.replace(/\,/g,'|');
					param = $.extend(param,{acceptFileTypes:'/'+acceptFileTypes+'$/i'});
				}
				if(!utils.isEmpty(maxFileSize) && utils.regexInteger(maxFileSize)) {
					param = $.extend(param,{maxFileSize:maxFileSize});
				}
				$this.jqueryFileUpload(param);
			}
		}
	});
}

/**
 * 监听日期时间标识
 * 标识
 * class="mix-datetime"(yyyy-mm-dd hh:ii:ss)或
 * class="mix-date"(yyyy-mm-dd) 或
 * class="mix-time" (hh:ii:ss)
 * 可选参数
 * data-start-date="" 设置时间范围--开始时间
 * date-end-date=""   设置时间范围--结束时间
 */
MixElementListener.inputDateListener = function() {
	$(".mix-datetime,.mix-date,.mix-time").each(function(){
		  var $this = $(this);
		  if(!utils.isContain($this.attr("class"), "mix-datetime-listener") && 
				  !utils.isContain($this.attr("class"), "mix-date-listener") && 
				  !utils.isContain($this.attr("class"), "mix-time-listener")) {
			  var $formGroup = $(this).parents(".form-group:eq(0)");
			  var classParentNames = $formGroup.attr("class");
			  var classNames = $this.attr("class");
			  if(!utils.isContain(classParentNames,'has-feedback') && (!utils.isContain(classNames, "mix-datetime-listener") 
					  || !utils.isContain(classNames, "mix-date-listener") || !utils.isContain(classNames, "mix-time-listener"))) {
				  if(utils.isEmpty(classParentNames)) {
					  $this.parent().addClass('has-feedback');
					  var top = 0;
					  var left = 0;
					  var pos = $this.position();
					  top = pos.top-3;
					  left = pos.left+($this.width()-18);
					  $this.after("<span class='glyphicon glyphicon-calendar inline-icon form-control-feedback' style='top:"+top+"px;left:"+left+"px'></span>");
					  var $icon = $this.find(".inline-icon");
				  } else {
					  $formGroup.addClass("has-feedback");
					  $this.after("<span class='glyphicon glyphicon-calendar form-control-feedback'></span>");
				  }
				  var setting = {};
				  if(utils.isContain(classNames,'mix-datetime')) {
					  setting = {format: 'yyyy-mm-dd hh:ii:ss',startView:2};
				  } else if(utils.isContain(classNames,'mix-date')) {
					  setting = {format: 'yyyy-mm-dd',minView: 2};
				  } else if(utils.isContain(classNames,'mix-time')) {
					  var date = new Date();
					  var month = date.getMonth()+1;
					  month = month<10?('0'+month):month;
					  var day = date.getDate();
					  day = day<10?('0'+day):day;
					 var startDate = date.getFullYear()+"-"+month+"-"+day;
					  setting = {format: 'hh:ii:ss',startDate:startDate,startView: 1,minView: 0,maxView:1};
				  }
				  var startDate = $this.data("start-date");
				  var endDate = $this.data("end-date");
				  if(!utils.isEmpty(startDate)) {
					  setting = $.extend(setting,{startDate:startDate});
				  }
				  if(!utils.isEmpty(endDate)) {
					  setting = $.extend(setting,{endDate:endDate});
				  }
				  var pickerHeight = 230;
				  var offset = $(this).offset();
				  var windowHeight = $(window).height();
				  if((offset.top+pickerHeight)>windowHeight) {
					  setting = $.extend(true, setting,{pickerPosition:'top-right'});
				  }
				  setting = $.extend(true, setting,{language:'zh-CN',autoclose:true});
				  $this.datetimepicker(setting).on('show',function(){
					  $this.prop("readonly",true);
				  }).on('hide',function(){
					  $this.prop("readonly",false);
				  });
				  if(utils.isContain($this.attr("class"), "mix-datetime")) {
					  $this.addClass("mix-datetime-listener");
				  } else if(utils.isContain($this.attr("class"), "mix-date")) {
					  $this.addClass("mix-date-listener");
				  } else if(utils.isContain($this.attr("class"), "mix-time")) {
					  $this.addClass("mix-time-listener");
				  }
			  }
		  }
	  });
}

/**
 * 下拉列表监听
 * 标识
 *   class="mix-select"
 *  参数
 *    必须
 *       data-uri 提供数据uri(数据来源)；数据格式为json数组格式;第一个字段为ID;第二个字段为名称
 *    可选
 *       data-default-value  默认值
 */
MixElementListener.selectListener = function() {
	$(".mix-select").each(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-select-listener")) {
			$this.addClass("mix-select-listener");
			var uri = $this.data("uri");
			var defaultValue = $this.data("default-value");
			var defaultValue = $this.data("default-value");
			if(!utils.isEmpty(uri)) {
				utils.selectItem(this, uri, defaultValue);
			}
		}
	});
}


/**
 * 级联下拉列表监听
 * 标识
 *   class="mix-cascade-select"
 *  参数
 *    必须
 *       data-uri 提供数据uri(数据来源)；数据格式为json数组格式;第一个字段为ID;第二个字段为名称
 *       data-cascade-id 级联的ID值
 *    可选
 *       data-default-value  默认值
 *       data-param-name 参数名称
 *       data-change-id 父级的数据改变时，要获取值的ID；如果data-cascade-id和data-change-id为同一个ID时，data-change-id属性可以不设置
 */
MixElementListener.cascadeSelectListener = function() {
	$(".mix-cascade-select").each(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-cascade-select-listener")) {
			$this.addClass("mix-cascade-select-listener");
			var uri = $this.data("uri");
			var defaultValue = $this.data("default-value");
			var cascadeId = $this.data("cascade-id");
			var paramName = $this.data("param-name");
			var changeId = $this.data("change-id");
			changeId = utils.isEmpty(changeId)?null:changeId;
			if(!utils.isEmpty(uri) && !utils.isEmpty(cascadeId)) {
				utils.selectCascadeItem(this,cascadeId, uri,paramName, defaultValue,changeId);
			}
		}
	});
}


/**
 * 输入表单实现下拉列表 <br />
 * 标识 <br />
 *    class="mix-input-select" <br />
 *  参数  <br />
 *     必须 <br />
 *       data-uri 提供数据uri(数据来源)；数据格式为json数组格式;第一个字段为ID;第二个字段为名称 <br />
 *     可选 <br />
 *       data-is-show-all 是否显示"全部链接";默认为yes,可设置的值为:"yes"或"no" <br />
 *       data-is-show-none 是否显示“无”
 *       data-param-name 搜索的参数名 <br />
 *       data-default-value 默认值
 */
MixElementListener.inputSelectListener = function() {
	$(".mix-input-select").each(function() {
		var $this= $(this);
		if(!utils.isContain($this.attr("class"), "mix-input-select-listener")) {
			$(this).addClass("mix-input-select-listener");
			var uri = $this.data("uri");
			if(!utils.isEmpty(uri)) {
				var paramName = $this.data("param-name");
				var defaultValue = $this.data("default-value");
				if(utils.isEmpty(defaultValue))
					defaultValue = $this.val();
				var isShowAll = $(this).data("is-show-all");
				isShowAll = (isShowAll == 'yes'?true:false);
				var isShowNone = $(this).data("is-show-none");
				isShowNone = (isShowNone == 'yes'?true:false);
				var setting = {uri:uri,isShow:false,isShowAll:isShowAll,isShowNone:isShowNone};
				if(!utils.isEmpty(paramName)) 
					setting = $.extend(setting,{paramName:paramName});
				if(!utils.isEmpty(defaultValue)) 
					setting = $.extend(setting,{defaultValue:defaultValue});
				$this.inputSelect(setting);
		   }
			$(this).on("click focus",function(event){
				$(this).prop("readonly",true);
				if(!utils.isEmpty(uri)) {
					var paramName = $(this).data("param-name");
					var defaultValue = $(this).data("default-value");
					var isShowAll = $(this).data("is-show-all");
					isShowAll = (isShowAll == 'yes'?true:false);
					var setting = {uri:uri,isShow:true};
					if(!utils.isEmpty(paramName)) 
						setting = $.extend(setting,{paramName:paramName});
					if(!utils.isEmpty(defaultValue)) 
						setting = $.extend(setting,{defaultValue:defaultValue});
					$(this).inputSelect(setting);
				}
				event.stopPropagation();
			});
		}
	});
}

/**
 * 输入表单实现下拉列表 <br />
 * 标识 <br />
 *    class="mix-input-select-relate" <br />
 *  参数  <br />
 *     必须 <br />
 *       data-uri 提供数据uri(数据来源)；数据格式为json数组格式;第一个字段为ID;第二个字段为名称 <br />
 *     可选 <br />
 *       data-is-show-all 是否显示"全部链接";默认为yes,可设置的值为:"yes"或"no" <br />
 *       data-param-name 搜索的参数名 <br />
 *       data-default-value 默认值
 */
MixElementListener.inputSelectRelateListener = function() {
	$(".mix-input-select-relate").each(function() {
		var $this= $(this);
		if(!utils.isContain($this.attr("class"), "mix-input-select-relate-listener")) {
			$(this).addClass("mix-input-select-relate-listener");
			$(this).on("click focus",function(event){
				$(this).prop("readonly",true);
				var uri = $this.data("uri");
				if(!utils.isEmpty(uri)) {
					var paramName = $this.data("param-name");
					var defaultValue = $this.data("default-value");
					if(utils.isEmpty(defaultValue))
						defaultValue = $this.val();
					var isShowAll = $(this).data("is-show-all");
					isShowAll = (isShowAll == 'yes'?true:false);
					var isShowNone = $(this).data("is-show-none");
					isShowNone = (isShowNone == 'yes'?true:false);
					var setting = {uri:uri,isShow:true,isShowAll:isShowAll,isShowNone:isShowNone,
							selectCallback:function(obj,datas){
								if(utils.isNotEmpty(datas) && datas.length>2) {
									 var start = 2;
									 var otherValue = datas;
									 if(utils.isNotEmpty(otherValue)) {
										 var len = otherValue.length-start;
										 var $parentDiv = $this.parents("td:eq(0)");
										 var isTd = true;
										 if(utils.isEmpty($parentDiv.html())) {
											 isTd = false;
											 $parentDiv = $this.parents(".form-group:eq(0)").parent();
										 }
										 for (var i = 0; i < len; i++) {
											 var $input = $parentDiv.next().find(".form-control");
											 if(utils.isNotEmpty($input.attr("class"))) {
												 $input.val(otherValue[i+start]);
											 } else {
												 $input = $parentDiv.next().next().find(".form-control");
												 if(utils.isNotEmpty($input.attr("class"))) { 
													 $input.val(otherValue[i+start]);
												 } else {
													 break;
												 }
											 }
											 if(isTd) {
												 $parentDiv = $input.parents("td:eq(0)");
											 } else {
												 $parentDiv = $input.parents(".form-group:eq(0)").parent();
											 }
										}
									 }
							 }
					}};
					if(!utils.isEmpty(paramName)) 
						setting = $.extend(setting,{paramName:paramName});
					if(!utils.isEmpty(defaultValue)) 
						setting = $.extend(setting,{defaultValue:defaultValue});
					$this.inputSelect(setting);
			   }
			  event.stopPropagation();
			});
		}
	});
}

/**
 * 输入框自动完成监听 <br />
 * 标识 <br /> 
 *   class="mix-auto-complete" <br />
 *   参数  <br />
 *     必须 <br />
 *       data-uri 提供数据uri(数据来源)；数据格式为json数组格式;第一个字段为ID;第二个字段为名称 <br />
 *     或data-json-data 提供数据uri(数据来源)；数据格式为json数组格式;第一个字段为ID;第二个字段为名称;第三个为显示内容; <br />
 *       “data-uri”属性的优先级高于“data-json-data”属性; <br />
 *     可选 <br />
 *       data-multiple 是否输入多个输入值;1--是；0--否，默认为：0 <br />
 *       data-multiple-separator 多个值时，多个值之间的分隔符；默认为英文分号:";"；<br />
 *       “data-multiple”与“data-multiple-separator”成对出现 
 */
MixElementListener.autoCompleteListener = function() {
	$(".mix-auto-complete").each(function() {
		var $this= $(this);
		if(!utils.isContain($this.attr("class"), "mix-auto-complete-listener")) {
			$this.addClass("mix-auto-complete-listener");
			var uri = $this.data("uri");
			var jsonData = $this.data("json-data");
			var multiple = $this.data("multiple");
			var multipleSeparator = $this.data("multiple-separator");
			var options = {};
			var is = false;
			if(!utils.isEmpty(uri)) {
				options=$.extend(options,{uri:uri});
				is = true;
			}
			if(!is && !utils.isEmpty(jsonData)) {
				options=$.extend(options,{jsonData:jsonData});
				is = true;
			}
			if(is && !utils.isEmpty(multiple) && (multiple=='1' || multiple=='0')) 
				options=$.extend(options,{multiple:(multiple==1?true:false)});
			if(is && !utils.isEmpty(multiple) && multiple=='1' && !utils.isEmpty(multipleSeparator)) 
				options=$.extend(options,{multipleSeparator:multipleSeparator});
			if(is)
				$this.autoComplete(options);
		}
	});
}

/**
 * 输入框自动完成关联监听 <br />
 * 标识 <br /> 
 *   class="mix-auto-complete-relate" <br />
 *   参数  <br />
 *     必须 <br />
 *       data-uri 提供数据uri(数据来源)；数据格式为json数组格式;第一个字段为ID;第二个字段为名称 <br />
 *     或data-json-data 提供数据uri(数据来源)；数据格式为json数组格式;第一个字段为ID;第二个字段为名称;第三个为显示内容; <br />
 *       “data-uri”属性的优先级高于“data-json-data”属性; <br />
 */
MixElementListener.autoCompleteRelateListener = function() {
	$(".mix-auto-complete-relate").each(function() {
		var $this= $(this);
		if(!utils.isContain($this.attr("class"), "mix-auto-complete-relate-listener")) {
			$this.addClass("mix-auto-complete-relate-listener");
			var uri = $this.data("uri");
			var jsonData = $this.data("json-data");
			var options = {
					selectCallback:function(event,data){
						 if(utils.isNotEmpty(data)) {
							 var otherValue = data.item.otherValue;
							 if(utils.isNotEmpty(otherValue)) {
								 var len = otherValue.length;
								 var $parentDiv = $this.parents("td:eq(0)");
								 var isTd = true;
								 if(utils.isEmpty($parentDiv.html())) {
									 isTd = false;
									 $parentDiv = $this.parents(".form-group:eq(0)").parent();
								 }
								 for (var i = 0; i < len; i++) {
									 var $input = $parentDiv.next().find(".form-control");
									 if(utils.isNotEmpty($input.attr("class"))) {
										 $input.val(otherValue[i]);
									 } else {
										
										 $input = $parentDiv.next().next().find(".form-control");
										 if(utils.isNotEmpty($input.attr("class"))) { 
											 $input.val(otherValue[i]);
										 } else {
											 break;
										 }
									 }
									 if(isTd) {
										 $parentDiv = $input.parents("td:eq(0)");
									 } else {
										 $parentDiv = $input.parents(".form-group:eq(0)").parent();
									 }
								}
							 }
						 }
					 }
			};
			var is = false;
			if(!utils.isEmpty(uri)) {
				options=$.extend(options,{uri:uri});
				is = true;
			}
			if(!is && !utils.isEmpty(jsonData)) {
				options=$.extend(options,{jsonData:jsonData});
				is = true;
			}
			if(is)
				$this.autoComplete(options);
		}
	});
}

/**
 * 微调数据 数据类型为number <br />
 * 标识 <br />
 *   class="mix-num-spinner" <br />
 *   参数  <br />
 *     可选 <br />
 *       data-min 最小值 <br />
 *       data-max 最大值 <br />
 *       data-step 每一次变化数值
 */
MixElementListener.spinnerNumListener = function() {
	$(".mix-num-spinner").each(function() {
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-num-spinner-listener")) {
			$this.addClass("mix-num-spinner-listener");
			var min = $this.data("min");
			min = utils.isEmpty(min)?0:(utils.regexNum(min)?min:0);
			var max = $this.data("max");
			max = utils.isEmpty(max)?0:(utils.regexNum(max)?max:0);
			var step = $this.data("step");
			step = utils.isEmpty(step)?1:(utils.regexNum(step)?step:1);
			var options = {min:min,step:step,numberFormat:"n",change: function(event, ui ) {
				var value = $(event.target).val();
				if(!utils.regexNum(value) || value<min || (max>0 && value>max)) {
					utils.showFailMsg("输入的数据格式错误！");
					$this.focus();
				}
			}};
			if(max>0) {
				options = $.extend(options,{max:max});
			}
			$(this).spinner(options);
		}
	});
}


/**
 * URL载入标记监听 <br />
 * 标识 <br />
 *   class="mix-load-url" <br />
 * 参数  <br />
 *   必须 <br />
 *    data-uri 提供数据uri
 */
MixElementListener.loadUrlListener = function() {
	var that = this;
	$(".mix-load-url").each(function() {
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-load-url-listener")) {
			$this.addClass("mix-load-url-listener");
			var uri = $this.data("uri");
			if(!utils.isEmpty(uri)) {
				$this.append('<div class="loading"><i class="fa fa-spinner fa-spin fa-lg"></i> 正在加载，请稍候...</div>');
				$this.load(uri,function() {
					$this.find(".loading").remove();
					MixElementListener.initEvent();
					MixElementListener.loadUrlListener();
				});
			}
			return false;
		}
	});
}


/**
 * 处理回车提交表单功能
 */
MixElementListener.handleEntrySubmit = function() {
	$("input").keydown(function(e){
		if(e.keyCode == 13) {
			var $form = $(this).parents("form:eq(0)");
			var $btn = $form.find(".mix-search-submit,.mix-data-submit,.login-btn");
			if(!utils.isEmpty($btn.attr("class"))) {
				$btn.trigger("click");
				return false;
			}
		}
	});
}

/**
 * 数据提交监听
 * 标识 <br />
 *   class="mix-load-url" <br />
 */
MixElementListener.submitDataListener = function() {
	$(".mix-post-data").click(function(){
		var $this = $(this);
		if(!utils.isContain($this.attr("class"), "mix-post-data-listener")) {
			$this.addClass("mix-post-data-listener");
			var uri = $(this).data("uri");
			var params = $(this).data("param");
			params = utils.isEmpty(params)?"":params;
			var refreshUri = $(this).data("refresh-uri");
			var target = $(this).data("target");
			var delAlertMsg = $(this).data("del-alert");
			if(!utils.isEmpty(uri)) {
				if(!utils.isEmpty(delAlertMsg)) {
					BootstrapDialogUtil.confirmDialog(delAlertMsg,function(){
						MixSmart.postData(uri,params,target,refreshUri);
					});
					return false;
				}
				MixSmart.postData(uri,params,target,refreshUri);
			}
		}
		return false;
	});
}


/**
 * 初始化监听插件
 * 
 */
MixElementListener.initEvent = function() {
	this.formRequireListener();
	this.submitDataListener();
	
	this.panelTreeListener();
	this.panelCheckTreeListener();
	this.panelOrgTreeListener();
	this.submitBtnListener();
	this.searchSubmitListener();
	this.limitHeightListener();
	
	this.hrefListener();
	this.uploadFileListener();
	this.selectListener();
	this.cascadeSelectListener();
	this.loadUrlListener();
	this.handleEntrySubmit();
	this.spinnerNumListener();
	this.inputPluginEvent();
}

/**
 * 输入框插件监听集合
 */
MixElementListener.inputPluginEvent = function() {
	this.inputTreeListener();
	this.inputOrgTreeListener();
	this.inputDateListener();
	this.inputSelectListener();
	this.inputSelectRelateListener();
	this.autoCompleteListener();
	this.autoCompleteRelateListener();
}

/**
 * 通过属性打开一个窗口
 * @param obj
 * @param op
 * @param flag
 */
MixElementListener.openProp = function(obj,op,flag) {
	if(!utils.isEmpty(obj)) {
		var $obj = null;
		if(typeof(obj) === 'string') {
			$obj = $(obj);
		} else {
			$obj = obj;
		}
		var uri = $obj.data("uri");
		var title = $obj.data("title");
		var value = $obj.attr("selected-value");
		var busiName = $obj.data("busi");
		var paramName = $obj.data("param-name");
		var selectedType = $obj.data("selected-type");
		selectedType = utils.isEmpty(selectedType)?'none-selected':selectedType;
		var width = $obj.data("width");
		width = utils.regexNum(width)?width:600;
		paramName = utils.isEmpty(paramName)?'id':paramName;
		if(!utils.isEmpty(selectedType)) {
			if(selectedType=='one-selected') {
				if(!utils.isEmpty(value) && value.indexOf(',')>0){
					BootstrapDialogUtil.warningAlert("只能选择一条数据!");
					return;
				} else if(utils.isEmpty(value)) {
					BootstrapDialogUtil.warningAlert("请选择一条数据!");
					return;
				}
			} else if(selectedType== 'multi-selected') {
				if(utils.isEmpty(value)) {
					BootstrapDialogUtil.warningAlert("请选择数据!");
					return;
				}
			} else {
				value = utils.isEmpty(value)?"":value;
			}
			var params = '';
			if(!utils.isEmpty(value)) {
				params = paramName+"="+value;
			}
			if(!utils.isEmpty(busiName)) {
				params += "&busiName="+busiName;
				if(!utils.isEmpty(op)) {
					params = params+"&op="+op;
				}
			}
			if(!utils.isEmpty(uri) ) {
				if(!utils.isEmpty(params)) {
					if(utils.isContain(uri, "?")) {
						uri = uri+"&"+params;
					} else {
						uri = uri+"?"+params;
					}
			    }
				if(!utils.isEmpty(flag) && flag == 'open-self') {
					//loadLocation(uri);
					openTab(title, uri,true);
				} else {
					BootstrapDialogUtil.loadUriDialog(title,uri,width,"#fff",false,function(){
						MixElementListener.initEvent();
					});
				}
			}
		}
	}
}

$(document).ready(function(){
	//setTimeout('MixElementListener.initEvent()', 300);
});